package com.heima.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.mapper.NodeMapper;
import com.heima.pojo.dto.NodePageQuery;
import com.heima.pojo.domain.Node;
import com.heima.pojo.domain.NodePageVo;
import com.heima.pojo.domain.PageResult;
import com.heima.pojo.vo.VmNodeVo;
import com.heima.service.NodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/vm-service/node")
public class NodeController {

    @Autowired
    private NodeService nodeService;
    @Autowired
    private NodeMapper nodeMapper;

    /**
     * 获取点位数量
     * @return
     */
    @GetMapping("/count")
    public Integer countNode(){
        return nodeMapper.countNode();
    }

    @GetMapping("/search")
    public PageResult<NodePageVo> search( NodePageQuery npq){
        PageResult<NodePageVo> pageResult= nodeService.search(npq);
        return pageResult;
    }

    @PostMapping
    public Boolean post(@RequestBody Node node){
        return nodeService.save(node);
    }

    /**
     * 获取点位详情
     * @param nodeId
     * @return
     */

    @GetMapping("/vmList/{nodeId}")
    public List<VmNodeVo> vmList(@PathVariable String nodeId){
        return nodeService.getVmList(nodeId);
    }

    /**
     * 修改点位
     */
    @PutMapping("/{nodeId}")
    public Boolean update(@PathVariable String nodeId ,@RequestBody Node node){
        return nodeService.update(node, Wrappers.<Node>lambdaUpdate().eq(Node::getId,nodeId));
    }

    /**
     * 删除点位
     */
    @DeleteMapping("/{nodeId}")
    public Boolean del(@PathVariable String nodeId){
        return nodeService.remove(Wrappers.<Node>lambdaQuery().eq(Node::getId,nodeId));
    }
}
